package com.lkd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Strings;
import com.lkd.common.VMSystem;
import com.lkd.entity.UserEntity;
import com.lkd.http.vo.LoginReq;
import com.lkd.http.vo.LoginResp;
import com.lkd.service.LoginService;
import com.lkd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Author : Bing
 * @Date 2023/3/1 19:31
 */
@Service
public class EmployeeLoginServiceImpl implements LoginService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private UserService userService;

    /**
     * 运维/运营人员登录
     * @param req
     * @return
     * @throws IOException
     */
    @Override
    public LoginResp Login(LoginReq req) throws IOException {
        LoginResp resp = new LoginResp();
        resp.setSuccess(false);
        String code =redisTemplate.boundValueOps(req.getMobile()).get();
        if(Strings.isNullOrEmpty(code)){
            resp.setMsg("验证码错误");
            return resp;
        }
        if(!req.getCode().equals(code)){
            resp.setMsg("验证码错误");
            return resp;
        }

        QueryWrapper<UserEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(UserEntity::getMobile, req.getMobile());
        UserEntity userEntity = userService.getOne(qw);
        if (userEntity == null){
            resp.setMsg("不存在该账户");
            return resp;
        }
        return okResp( userEntity,VMSystem.LOGIN_EMP );
    }

    /**
     * 获取登录类型
     * @return 登录类型对应的枚举
     */
    @Override
    public Integer getLoginType() {
        return VMSystem.LOGIN_EMP;
    }
}
